/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.mainApp;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

import org.newdawn.slick.SlickException;

import ca.usherbrooke.mainApp.view.MainPanel;
import ca.usherbrooke.mainApp.view.simulation.PanelSimulation;
import ca.usherbrooke.utils.ExceptionTool;

/**
 * @author Cody Stoutenburg
 * 
 */
public class MainFrame extends JFrame implements WindowListener {
	private static final long serialVersionUID = 1L;

	private final MainAppAgent agent;

	private final MainPanel mainPanel;

	public MainFrame(MainAppAgent agent) {
		super("Virtual World for Intelligent Agent");

		this.agent = agent;

		mainPanel = new MainPanel(this.agent);

		this.add(mainPanel);

		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		this.setVisible(true);

		this.addWindowListener(this);

		this.setMinimumSize(new Dimension(800, 600));
	}

	public MainPanel getMainPanel() {
		return this.mainPanel;
	}

	@Override
	public void windowOpened(WindowEvent e) {
	}

	@Override
	public void windowClosing(WindowEvent e) {

		Component[] comps = this.getMainPanel().getCenterPanel()
				.getComponents();

		for (Component component : comps) {
			if (component instanceof PanelSimulation) {
				try {
					((PanelSimulation) component).stop();
				} catch (SlickException ex) {
					ExceptionTool.showError("Fail to stop the application", ex);
				}
			}
		}
		this.setVisible(false);

		this.agent.stopJade();
	}

	@Override
	public void windowClosed(WindowEvent e) {
	}

	@Override
	public void windowIconified(WindowEvent e) {
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
	}

	@Override
	public void windowActivated(WindowEvent e) {
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
	}
}
